<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 模块管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="模块名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-lx-add" @click="addModule">新增</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="模块名"></el-table-column>
                <el-table-column prop="tester" label="测试人员" align="center"></el-table-column>
                <el-table-column prop="project_name" label="所属项目" align="center"></el-table-column>
                <el-table-column prop="desc" label="简要描述" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-lx-hot"
                            @click="handleRun(scope.$index, scope.row)"
                        >运行</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="模块名"
                    prop="name"
                    :rules="[
                    { required: true, message: '模块名不能为空'}
                    ]"
                >
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="测试人员">
                    <el-input v-model="form.tester"></el-input>
                </el-form-item>
                <el-form-item label="所属项目">
                    <el-select v-model="project" placeholder="选择项目" style="width:100%">
                        <el-option v-for="item in projects"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id + '+' + item.name">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="简要描述">
                    <el-input v-model="form.desc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">提 交</el-button>
            </span>
        </el-dialog>

        <!-- 新增项目弹框 -->
        <el-dialog title="新增模块" :visible.sync="addVisible" width="40%">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="模块名"
                    prop="name"
                    :rules="[
                    { required: true, message: '模块名不能为空'}
                    ]"
                >
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="测试人员"
                    prop="tester"
                    :rules="[
                    { required: true, message: '测试人员不能为空'}
                    ]"
                >
                    <el-input v-model="form.tester"></el-input>
                </el-form-item>
                <el-form-item label="所属项目">
                    <el-select v-model="project" placeholder="选择项目" style="width:100%">
                        <el-option v-for="item in projects"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id + '+' + item.name">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="简要描述">
                    <el-input v-model="form.desc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('form')">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

import { modulars_list } from '../../api/api';
import { add_modulars } from '../../api/api';
import { edit_modulars } from '../../api/api';
import { projects_names } from '../../api/api';
import { delete_modular } from '../../api/api';




export default {
    name: 'basetable',
    data() {
        return {
            query: {
                page: 1,
                size: 10,
                name: ""
            },
            tableData: [],
            projects: [],
            multipleSelection: [],
            delList: [],
            addVisible: false,
            editVisible: false,
            pageTotal: 0,
            form: {
                name: "",
                tester: "",
                project_id: "",
                project_name: "",
                desc: ""
            },
            project: "",
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
        this.getProjectList();
    },
    methods: {
        // 查询数据
        getData() {
            modulars_list(this.query).then(res => {
                this.tableData = res.data.data.list;
                this.pageTotal = res.data.data.total;
            }).catch(error => {
                console.log(error);
            });
        },

        getProjectList() {
            projects_names(this.query)
            .then(response => {
                this.projects = response.data.data;
            }).catch(error => {
                console.log(error);
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.getData();
        },

        // 触发新增项目按钮
        addModule() {
            this.addVisible = true;
        },

        // 触发保存新增项目按钮
        submitForm(form) {
            this.form.project_id = this.project.split("+")[0];
            this.form.project_name = this.project.split("+")[1];
            this.$refs[form].validate((valid) => {
            if (valid) {
                add_modulars(this.form)
                .then(response => {
                    if (response.data.success == true){
                        this.addVisible = false;
                        this.$message.success(response.data.message);
                        this.getData();
                        this.form = {
                            name: "",
                            tester: "",
                            project_id: "",
                            project_name: "",
                            desc: ""
                        },
                        this.project = "";
                    }else {
                        this.$message.error(response.data.message);
                    }
                })
                .catch(error => {
                    this.$message.error(error);
                })
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },

        // 删除操作
        handleDelete(index, row) {
            this.modulars_id = row.id;
            this.modulars_name = row.name;
            console.log("ffffff");
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    console.log("ffffff");
                    delete_modular(this.modulars_id)
                    .then(response => {
                        console.log("ffffff");
                        console.log(response.data)
                        this.$message.success(`删除成功: ${this.modulars_name}`);
                        this.tableData.splice(index, 1);
                    }).catch(error => {});
                }).catch(() => {});
        },

        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.project = row.project_name;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit(form) {
            this.form.project_id = this.project.split("+")[0];
            this.form.project_name = this.project.split("+")[1];
            this.$refs[form].validate((valid) => {
            if (valid) {
                edit_modulars(this.form.id,this.form)
                .then(response => {
                    if (response.data.success == true){
                        this.editVisible = false;
                        this.$message.success(`修改 ${this.form.name} 成功`);
                        this.getData();
                        this.form = {
                            name: "",
                            tester: "",
                            project_id: "",
                            project_name: "",
                            desc: ""
                        },
                        this.project = "";
                    }else {
                        for(var o in response.data.message){ 
                            this.$message.error(response.data.message);
                        } 
                    }
                })
                .catch(error => {
                    this.$message.error(error);
                })
            } else {
                console.log('error submit!!');
                return false;
            }
            });
            this.$set(this.tableData, this.idx, this.form);
        },

        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
